package zero.tech.games.phz.util;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.alibaba.fastjson.JSONObject;

import zero.tech.core.ZeroContext;
import zero.tech.core.util.RandomUtil;
import zero.tech.games.common.BigCalSource;
import zero.tech.games.common.QueueConsts;
import zero.tech.games.common.playback.Playback;
import zero.tech.games.common.playback.PlaybackBasic;
import zero.tech.games.entity.games.Club;
import zero.tech.games.logic.common.Room;
import zero.tech.games.logic.common.RoomCache;
import zero.tech.games.logic.data.res.ResSeat;
import zero.tech.games.logic.util.PckUtil;
import zero.tech.games.phz.data.PhzBigPanel;
import zero.tech.games.phz.data.PhzGameBase;
import zero.tech.games.phz.data.PhzRoomCache;
import zero.tech.games.phz.data.PhzSmallPanel;
import zero.tech.games.phz.data.entity.Card;
import zero.tech.games.phz.data.entity.PlayerData;
import zero.tech.games.phz.data.entity.PlayerSmallCal;
import zero.tech.games.phz.data.playback.PhzPlaybackBigOverStep;
import zero.tech.games.phz.data.playback.PhzPlaybackFapaiStep;
import zero.tech.games.phz.data.playback.PhzPlaybackOperateStep;
import zero.tech.games.phz.data.playback.PhzPlaybackSmallOverStep;
import zero.tech.redis.queue.QueueUtil;
import zero.tech.wsnet.controller.LogicController;

public class PlaybackUtil {

	public static void saveClubBigPlayback(Room room, int source) {
		Club club=room.getClub();
		if(club!=null) {
			RoomCache roomCache = room.getRoomCache();
			if(source==BigCalSource.SMALLOVER_BIG_CAL) {
				int club_id=club.getClub_id();
				int game_id=room.getGame_id();
				String room_id=room.getRoom_id();
				String config_str=room.getConfigStr();
				int room_times=room.getRoom_times();
				String play_code=RandomUtil.randNum(8);
				Playback playback=new Playback();
				playback.setCreate_stamp(new Date());
				playback.setGame_id(game_id);
				playback.setClub_id(club_id);
				playback.setRoom_id(room_id);
				playback.setRoom_times(room_times);
				playback.setPlay_code(play_code);
				playback.setConfig_str(config_str);
				playback.setFapai_step(packageFapaiStep(room));
				playback.setOperate_step(packageOperateStep(room));
				playback.setSmallover_step(packageSmallOverStep(room));
				playback.setBigover_step(packageBigOverStep(room));
				playback.getUids().addAll(room.getAllControllers().keySet());
				PhzSmallPanel phzSmallPanel = (PhzSmallPanel) roomCache.getSmallPanels().get(room_times);
				List<PlayerSmallCal> list=phzSmallPanel.getPlayerCals();
				for(PlayerSmallCal playerSmallCal:list) {
					playback.getBasics().add(packagePlaybackBasic(playerSmallCal));
				}
				roomCache.getPlaybacks().add(playback);
			}
			QueueUtil queueUtil=ZeroContext.getInstance(QueueUtil.class);
			queueUtil.putQueueBatch(QueueConsts.QUEUE_PLAYBACK,roomCache.getPlaybacks());
		}
	}
	public static void saveClubSmallPlayback(Room room) {
		Club club=room.getClub();
		if(club!=null) {
			RoomCache roomCache = room.getRoomCache();
			int club_id=club.getClub_id();
			int game_id=room.getGame_id();
			String config_str=room.getConfigStr();
			String play_code=RandomUtil.randNum(8);
			String room_id=room.getRoom_id();
			int room_times=room.getRoom_times();
			Playback playback=new Playback();
			playback.setCreate_stamp(new Date());
			playback.setClub_id(club_id);
			playback.setGame_id(game_id);
			playback.setRoom_id(room_id);
			playback.setRoom_times(room_times);
			playback.setConfig_str(config_str);
			playback.setPlay_code(play_code);
			playback.setFapai_step(packageFapaiStep(room));
			playback.setOperate_step(packageOperateStep(room));
			playback.setSmallover_step(packageSmallOverStep(room));
			playback.getUids().addAll(room.getAllControllers().keySet());
			PhzSmallPanel phzSmallPanel = (PhzSmallPanel) roomCache.getSmallPanels().get(room_times);
			List<PlayerSmallCal> list=phzSmallPanel.getPlayerCals();
			for(PlayerSmallCal playerSmallCal:list) {
				playback.getBasics().add(packagePlaybackBasic(playerSmallCal));
			}
			roomCache.getPlaybacks().add(playback);
		}
	}

	private static PlaybackBasic packagePlaybackBasic(PlayerSmallCal playerSmallCal) {
		PlaybackBasic playbackBasic= new PlaybackBasic();
		playbackBasic.setSimpleUser(playerSmallCal.getSimpleUser());
		playbackBasic.setCur_win_gold(playerSmallCal.getCur_win_gold());
		playbackBasic.setCur_win_score(playerSmallCal.getCur_win_score());
		return playbackBasic;
	}
	private static JSONObject packageBigOverStep(Room room) {
		PhzPlaybackBigOverStep phzPlaybackBigOverStep = new PhzPlaybackBigOverStep();
		RoomCache roomCache = room.getRoomCache();
		PhzBigPanel phzBigPanel=(PhzBigPanel) roomCache.getBigPanel();
		phzPlaybackBigOverStep.setPhzBigPanel(phzBigPanel);
		return (JSONObject) JSONObject.toJSON(phzPlaybackBigOverStep);
	}
	private static JSONObject packageSmallOverStep(Room room) {
		RoomCache roomCache = room.getRoomCache();
		int room_times=room.getRoom_times();
		PhzPlaybackSmallOverStep phzPlaybackSmallOverStep = new PhzPlaybackSmallOverStep();
		PhzSmallPanel phzSmallPanel = (PhzSmallPanel) roomCache.getSmallPanels().get(room_times);
		phzPlaybackSmallOverStep.setPhzSmallPanel(phzSmallPanel);
		return (JSONObject) JSONObject.toJSON(phzPlaybackSmallOverStep);
	}

	private static JSONObject packageOperateStep(Room room) {
		PhzPlaybackOperateStep phzPlaybackOperateStep = new PhzPlaybackOperateStep();
		//code here
		PhzGameBase phzGameBase = (PhzGameBase) room.getGameBase();
		phzPlaybackOperateStep.getOperateDatas().addAll(phzGameBase.getOperateDatas());
		return (JSONObject) JSONObject.toJSON(phzPlaybackOperateStep);
	}

	private static JSONObject packageFapaiStep(Room room) {
		PhzPlaybackFapaiStep phzPlaybackFapaiStep = new PhzPlaybackFapaiStep();
		//code here
		PhzGameBase phzGameBase = (PhzGameBase) room.getGameBase();
		Map<Integer,Integer> map =new HashMap<Integer, Integer>();
		map.putAll(room.getSeats());
		for(Entry<Integer,Integer> entry:map.entrySet()) {
			Integer pos=entry.getKey();
			Integer uid=entry.getValue();
			if(uid!=null) {
				LogicController logicController=room.getAllControllers().get(uid);
				ResSeat seat=PckUtil.packageResSeat(logicController);
				phzPlaybackFapaiStep.getRes_seats().put(pos,seat);
			}
		}
		for(Entry<Integer,List<Card>> handsmap:phzGameBase.getHandsmap().entrySet()) {
			int uid=handsmap.getKey();
			List<Card> cards=new ArrayList<>();
			if(handsmap.getValue()!=null&&!handsmap.getValue().isEmpty()) {
				cards.addAll(handsmap.getValue());
			}
			phzPlaybackFapaiStep.getHandsmap().put(uid, cards);
		}
		PhzRoomCache phzRoomCache = (PhzRoomCache) room.getRoomCache();
		phzPlaybackFapaiStep.getBirds().putAll(phzRoomCache.getBirds());
		Map<Integer, Integer> scoremap=phzRoomCache.getTotal_score();
		for(PlayerData pData:phzGameBase.getPlayers().values()) {
			int score=scoremap.containsKey(pData.getUid())?scoremap.get(pData.getUid()):0;
			phzPlaybackFapaiStep.getScoremap().put(pData.getUid(), score);
		}
		phzPlaybackFapaiStep.setDipai_count(phzGameBase.getDipai_count());
		phzPlaybackFapaiStep.setBanker_uid(phzGameBase.getBanker_uid());
		return (JSONObject) JSONObject.toJSON(phzPlaybackFapaiStep);
	}
}
